'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:UserFeedbackCtrl
 * @description
 * # UserFeedbackCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('UserFeedbackCtrl', function($scope, openApi, DataService, Util, $rootScope) {
        $scope.param = {};
        $scope.param.pics = [];
        $scope.type = [{
            text: "问题反馈",
            value: 1
        }, {
            text: "优化建议",
            value: 2
        }, {
            text: "新功能",
            value: 3
        }];
        var headers = {
            'Content-Type': undefined
        }
        $scope.submit = function() {
            if (!$scope.param.content) {
                Util.Toast("请填写反馈意见")
                return;
            };
            upload($scope.param)
                // var fd = new FormData();
                // // var xhr = new XMLHttpRequest();
                // fd.append("type", $scope.param.type);
                // fd.append("content", $scope.param.content);
                // fd.append("contactWay", $scope.param.contactWay);

            // $scope.param.photo1 = 1
            // console.log($scope.param)
            // DataService.getData(openApi.feedback, fd, headers).then(function(data) {
            //     console.log(data)
            // })
        };
        $scope.addpic = function() {
            if ($scope.param.pics.length > 2) {
                Util.Toast("只能上传三张图片")
                return
            }
            navigator.notification.confirm(
                '附件选择方式',
                choosePic,
                '添加附件', ['拍照', '相册']
            )
        }
        $scope.delpic = function(index) {
            $scope.param.pics.splice(index, 1)
        }
        var choosePic = function(btn) {
             Util.showLoadingStatus();
            var cameraOptions = {
                quality: 30,
                EncodingType: 0,
                destinationType: 0
            };
            cameraOptions.sourceType = btn
            if (btn == 0) {
                return
            }
            navigator.camera.getPicture(cameraSuccess, cameraError, cameraOptions);
        }
        var cameraSuccess = function(imageData) {
            Util.hideLoadingStatus()
            $scope.param.pics.push(imageData);
            $scope.$apply();
        }
        var cameraError = function() {
            Util.hideLoadingStatus()
        };
        $scope.param.type = 1;
        var upload = function(params) {
            var oData = new FormData(document.forms.namedItem("fileinfo"));
            var xhr = new XMLHttpRequest();
            var fd = new FormData();
            // 默认值
            $scope.param.deviceModel = window.device ? window.device.model : "";
            $scope.param.clientType = Util.getPlatform();
            fd.append("type", $scope.param.type);
            fd.append("clientType", $scope.param.clientType);
            fd.append("deviceModel", $scope.param.deviceModel);
            fd.append("content", $scope.param.content ? $scope.param.content : "");
            fd.append("contactWay", $scope.param.contactWay ? $scope.param.contactWay : "");
            fd.append("photo1File", $scope.param.pics[0] ? $scope.param.pics[0] : "");
            fd.append("photo2File", $scope.param.pics[1] ? $scope.param.pics[1] : "");
            fd.append("photo3File", $scope.param.pics[2] ? $scope.param.pics[2] : "");
            $rootScope.showLoading = true
            xhr.open("POST", openApi.feedback, true);
            xhr.onload = function(oEvent) {
                $rootScope.showLoading = false
                $scope.$apply()
                if (xhr.status == 200) {
                    var data = JSON.parse(xhr.response)
                    Util.Toast(data.result,{callback:Util.goBack})
                    console.log(xhr.response)
                } else {
                    Util.Toast("提交失败，请稍后尝试")
                }
            };

            xhr.send(fd);

        }

    });
